package com.doom.a3rdlibdemo;

import android.content.Context;
import android.util.Log;

import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.ext.junit.runners.AndroidJUnit4;

import com.doom.a3rdlibdemo.decorator.BasePizza;
import com.doom.a3rdlibdemo.decorator.Pizza;
import com.doom.a3rdlibdemo.decorator.PizzaA;
import com.doom.a3rdlibdemo.decorator.PizzaB;
import com.doom.a3rdlibdemo.decorator.PizzaC;

import org.junit.Test;
import org.junit.runner.RunWith;

import static org.junit.Assert.*;

/**
 * Instrumented test, which will execute on an Android device.
 *
 * @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
 */
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
    @Test
    public void useAppContext() {
        // Context of the app under test.
        Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
        assertEquals("com.doom.a3rdlibdemo", appContext.getPackageName());
    }

    @Test
    public void testPublishMessage(){
        // 发布消息 -> 接受消息
//        WebServer webServer = new WebServer();
//        Observer checker01 = new User("01");
//        Observer checker02 = new User("02");
//        Observer checker03 = new User("03");
//        Observer checker04 = new User("04");
//
//        webServer.add(checker01);
//        webServer.add(checker02);
//        webServer.add(checker03);
//        webServer.add(checker04);
//
//        webServer.publishMessage("HELLO");

        // 链式调用
//        Observable.create(new ObservableOnSubscribe<String>() {
//            @Override
//            public void subscribe(Emitter<String> emitter) {
//                emitter.onNext("Hello");
//            }
//        }).subscribe(new Observer() {
//            @Override
//            public void onSubscribe() {
//
//            }
//
//            @Override
//            public void onNext(Object o) {
//                Log.d("OBB", "recv: "+ (String)o);
//            }
//
//            @Override
//            public void onError(Throwable e) {
//
//            }
//
//            @Override
//            public void onComplete() {
//
//            }
//        });

        // 装饰器
//        Pizza x = new BasePizza();
//        PizzaA pizzaA = new PizzaA(new PizzaB(new PizzaC(x)));
//        pizzaA.show();

        Observable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(Emitter<String> emitter) {
                emitter.onNext("网络检测成功");
            }
        }).map(new Function<String, Object>() {

            @Override
            public Object apply(String s) {
                return s+"+数据库检测成功";
            }
        }).map(new Function<String, Object>() {
            @Override
            public Object apply(String s) {
                return s+"+文件系统检测成功";
            }
        }).map(new Function<String, Object>() {
            @Override
            public Object apply(String s) {
                Log.d("OBSERVER", "上游:"+Thread.currentThread().getName());
                return s+"+用户名密码验证成功";
            }
        }).subscribeOn()
                .observeOn().subscribe(new Observer() {
            @Override
            public void onSubscribe() {
                Log.d("OBSERVER", "onSubscribe:"+Thread.currentThread().getName());
            }

            @Override
            public void onNext(Object o) {
                Log.d("OBSERVER", "下游:"+Thread.currentThread().getName());
                Log.d("OBSERVER", "接收消息: "+ (String)o);
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });
    }


}